package com.fanghaoran.dao;

import com.fanghaoran.bean.Interaction;
import com.fanghaoran.entity.InfoToDisplay;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: win11
 * @Date: 2023/03/30/19:55
 * @Description: 用户端浏览其他用户信息业务的数据库操作
 */
public class BrowseDao {
    /**
     * 获取全部可用于展示的用户信息(部分个人信息+学习信息)
     */
    public Interaction getInfoToDisplay() throws Exception {
        String sql = "select user_info.username,gender,nickname,wordAccount,studyTime,clockDays from user_info " +
                "join study_info on user_info.id=study_info.userId " +
                "join sign_info on user_info.id=sign_info.userId where isPrivate=0;";
        List<InfoToDisplay> infoToDisplays = BaseDao.executeQuery(InfoToDisplay.class, sql);
        return new Interaction(null, infoToDisplays);
    }

    /**
     * 指定用户名获取可用于展示的用户信息(部分个人信息+学习信息)
     */
    public Interaction browseByUsername(String username) throws Exception {
        String sql = "select user_info.username,gender,nickname,wordAccount,studyTime,clockDays from user_info " +
                "join study_info on user_info.id=study_info.userId " +
                "join sign_info on user_info.id=sign_info.userId where username like ? and isPrivate=0;";
        List<InfoToDisplay> infoToDisplays = BaseDao.executeQuery(InfoToDisplay.class, sql, "%" + username + "%");
        return new Interaction(null, infoToDisplays);
    }

    /**
     * 指定性别获取可用于展示的用户信息(部分个人信息+学习信息)
     */
    public Interaction browseByGender(String gender) throws Exception {
        String sql = "select user_info.username,gender,nickname,wordAccount,studyTime,clockDays from user_info " +
                "join study_info on user_info.id=study_info.userId " +
                "join sign_info on user_info.id=sign_info.userId where gender like ? and isPrivate=0;";
        List<InfoToDisplay> infoToDisplays = BaseDao.executeQuery(InfoToDisplay.class, sql, "%" + gender + "%");
        return new Interaction(null, infoToDisplays);
    }

    /**
     * 指定昵称获取可用于展示的用户信息(部分个人信息+学习信息)
     */
    public Interaction browseByNickname(String nickname) throws Exception {
        String sql = "select user_info.username,gender,nickname,wordAccount,studyTime,clockDays from user_info " +
                "join study_info on user_info.id=study_info.userId " +
                "join sign_info on user_info.id=sign_info.userId where nickname like ? and isPrivate=0;";
        List<InfoToDisplay> infoToDisplays = BaseDao.executeQuery(InfoToDisplay.class, sql, "%" + nickname + "%");
        return new Interaction(null, infoToDisplays);
    }

    /**
     * 设置信息私密
     */
    public void setPrivate(String username) throws SQLException {
        String sql = "update user_info set isPrivate=1 where username=?;";
        BaseDao.executeUpdate(sql, username);
    }

    /**
     * 单词榜数据
     */
    public Interaction getWordRank() throws Exception {
        String sql = "select username,wordAccount from user_info " +
                "join study_info on user_info.id=study_info.userId " +
                "order by  wordAccount desc;";
        List<InfoToDisplay> infoToDisplays = BaseDao.executeQuery(InfoToDisplay.class, sql);
        return new Interaction(null, infoToDisplays);
    }

    /**
     * 连续打卡天数榜
     */
    public Interaction getClockDay() throws Exception {
        String sql = "select username,clockDays from sign_info " +
                "join user_info on sign_info.userId=user_info.id " +
                "order by clockDays desc;";
        List<InfoToDisplay> infoToDisplays = BaseDao.executeQuery(InfoToDisplay.class, sql);
        return new Interaction(null, infoToDisplays);
    }

}
